﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace DotNet.Data
{
    public class sendMailDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int SendID)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from sendMail where SendID=@SendID");
            htSyn.Add("SendID", SendID);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得邮件信息数据列表
        /// <summary>
        /// 获得邮件信息数据列表
        /// </summary>
        /// <returns></returns>
        public IList<sendMail> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select SendID,Company,sendName,sendEmail,sendTel,Country,Description,Readed,SCompany,CreateTime,Stitle from sendMail ");
            return getsendMailsBySql(strSql.ToString());
        }
        #endregion

        #region 根据邮件信息号查询实体
        /// <summary>
        /// 根据邮件信息号查询实体
        /// </summary>
        /// <param name="SendID"></param>
        /// <returns></returns>
        public sendMail GetModel(int SendID)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select SendID,Company,sendName,sendEmail,sendTel,Country,Description,Readed,SCompany,CreateTime,Stitle from sendMail ");
            strSql.Append(" where SendID=@SendID ");
            htSyn.Add("SendID", SendID);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            sendMail sendMail = new sendMail();
            if (dt.Rows.Count>0)
            {
                sendMail.SendID = Convert.ToInt32(dt.Rows[0]["SendID"]);
                sendMail.Company = dt.Rows[0]["Company"].ToString();
                sendMail.sendName = dt.Rows[0]["sendName"].ToString();
                sendMail.sendEmail = dt.Rows[0]["sendEmail"].ToString();
                sendMail.sendTel = dt.Rows[0]["sendTel"].ToString();
                sendMail.Country = dt.Rows[0]["Country"].ToString();
                sendMail.Description = dt.Rows[0]["Description"].ToString();
                sendMail.Readed = Convert.ToBoolean(dt.Rows[0]["Readed"]);
                sendMail.SCompany = dt.Rows[0]["SCompany"].ToString();
                sendMail.CreateTime = Convert.ToDateTime(dt.Rows[0]["CreateTime"]);
                sendMail.Stitle = dt.Rows[0]["Stitle"].ToString();
            }           
            return sendMail;
        }
        #endregion

        #region 修改邮件信息
        /// <summary>
        /// 修改邮件信息
        /// </summary>
        /// <param name="sendMail"></param>
        /// <returns></returns>
        public bool Update(sendMail sendMail)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update sendMail set ");
            strSql.Append("Company=@Company,");
            strSql.Append("sendName=@sendName,");
            strSql.Append("sendEmail=@sendEmail,");
            strSql.Append("sendTel=@sendTel,");
            strSql.Append("Country=@Country,");
            strSql.Append("Description=@Description,");
            strSql.Append("Readed=@Readed,");
            strSql.Append("SCompany=@SCompany,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Stitle=@Stitle");
            strSql.Append(" where SendID=@SendID ");
            htSyn.Add("@Company", sendMail.Company);
            htSyn.Add("@sendTel", sendMail.sendTel);
            htSyn.Add("@sendName", sendMail.sendName);
            htSyn.Add("@sendEmail",sendMail.sendEmail);
            htSyn.Add("@Country", sendMail.Country);
            htSyn.Add("@Description", sendMail.Description);
            htSyn.Add("@Readed", sendMail.Readed);
            htSyn.Add("@SCompany", sendMail.SCompany);
            htSyn.Add("@CreateTime", sendMail.CreateTime);
            htSyn.Add("@Stitle",sendMail.Stitle);
            htSyn.Add("@SendID", sendMail.SendID);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除邮件信息根据邮件信息号
        /// <summary>
        /// 删除邮件信息根据邮件信息号
        /// </summary>
        /// <param name="SendID"></param>
        /// <returns></returns>
        public bool Delete(int SendID)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from sendMail ");
            strSql.Append(" where SendID=@SendID ");
            htSyn.Add("SendID", SendID);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 添加邮件信息
        /// <summary>
        /// 添加邮件信息
        /// </summary>
        /// <param name="sendMail"></param>
        /// <returns></returns>
        public bool Insert(sendMail sendMail)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into sendMail(");
            strSql.Append("Company,sendName,sendEmail,sendTel,Country,Description,Readed,SCompany,CreateTime,Stitle)");
            strSql.Append(" values (");
            strSql.Append("@Company,@sendName,@sendEmail,@sendTel,@Country,@Description,@Readed,@SCompany,@CreateTime,@Stitle)");
            htSyn.Add("@Company", sendMail.Company);
            htSyn.Add("@sendTel", sendMail.sendTel);
            htSyn.Add("@sendName", sendMail.sendName);
            htSyn.Add("@sendEmail",sendMail.sendEmail);
            htSyn.Add("@Country", sendMail.Country);
            htSyn.Add("@Description", sendMail.Description);
            htSyn.Add("@Readed", sendMail.Readed);
            htSyn.Add("@SCompany", sendMail.SCompany);
            htSyn.Add("@CreateTime", sendMail.CreateTime);
            htSyn.Add("@Stitle", sendMail.Stitle);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<sendMail> getsendMailsBySql(string strSql)
        {
            IList<sendMail> list = new List<sendMail>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    sendMail sendMail = new sendMail();
                    sendMail.SendID = Convert.ToInt32(dr["SendID"]);
                    sendMail.Company = dr["Company"].ToString();
                    sendMail.sendName = dr["sendName"].ToString();
                    sendMail.sendEmail = dr["sendEmail"].ToString();
                    sendMail.sendTel = dr["sendTel"].ToString();
                    sendMail.Country = dr["Country"].ToString();
                    sendMail.Description = dr["Description"].ToString();
                    sendMail.Readed = Convert.ToBoolean(dr["Readed"]);
                    sendMail.SCompany = dr["SCompany"].ToString();
                    sendMail.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
                    sendMail.Stitle = dr["sendMail"].ToString();
                    list.Add(sendMail);
                }
            }
            return list;
        }
        #endregion
    }
}
